from datetime import datetime
from typing import Optional, List, Dict, Any

class User:
    def __init__(self, user_id: int, username: str, password: str, name: str, phone: str, 
                 budget: Optional[float] = None, preferred_type: Optional[str] = None,
                 created_at: Optional[str] = None):
        self.id = user_id
        self.username = username
        self.password = password
        self.name = name
        self.phone = phone
        self.budget = budget
        self.preferred_type = preferred_type
        self.created_at = created_at or datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'id': self.id,
            'username': self.username,
            'name': self.name,
            'phone': self.phone,
            'budget': self.budget,
            'preferred_type': self.preferred_type,
            'created_at': self.created_at
        }

class Consultation:
    def __init__(self, consultation_id: int, user_id: int, budget_range: str, preferred_type: str,
                 usage_scenario: str, fuel_type: str, brand_preference: str, 
                 consultation_content: str, llm_response: str, created_at: Optional[str] = None):
        self.id = consultation_id
        self.user_id = user_id
        self.budget_range = budget_range
        self.preferred_type = preferred_type
        self.usage_scenario = usage_scenario
        self.fuel_type = fuel_type
        self.brand_preference = brand_preference
        self.consultation_content = consultation_content
        self.llm_response = llm_response
        self.created_at = created_at or datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'id': self.id,
            'user_id': self.user_id,
            'budget_range': self.budget_range,
            'preferred_type': self.preferred_type,
            'usage_scenario': self.usage_scenario,
            'fuel_type': self.fuel_type,
            'brand_preference': self.brand_preference,
            'consultation_content': self.consultation_content,
            'llm_response': self.llm_response,
            'created_at': self.created_at
        }

class Points:
    def __init__(self, points_id: int, user_id: int, points: int, 
                 last_updated: Optional[str] = None):
        self.id = points_id
        self.user_id = user_id
        self.points = points
        self.last_updated = last_updated or datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'id': self.id,
            'user_id': self.user_id,
            'points': self.points,
            'last_updated': self.last_updated
        }

class PointRecord:
    def __init__(self, record_id: int, user_id: int, points_change: int, reason: str,
                 created_at: Optional[str] = None):
        self.id = record_id
        self.user_id = user_id
        self.points_change = points_change
        self.reason = reason
        self.created_at = created_at or datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'id': self.id,
            'user_id': self.user_id,
            'points_change': self.points_change,
            'reason': self.reason,
            'created_at': self.created_at
        }

class Admin:
    def __init__(self, admin_id: int, username: str, password: str,
                 created_at: Optional[str] = None):
        self.id = admin_id
        self.username = username
        self.password = password
        self.created_at = created_at or datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'id': self.id,
            'username': self.username,
            'created_at': self.created_at
        }

class ConsultationStats:
    def __init__(self, total_consultations: int, active_users: int, 
                 avg_response_length: float, popular_types: List[Dict[str, Any]]):
        self.total_consultations = total_consultations
        self.active_users = active_users
        self.avg_response_length = avg_response_length
        self.popular_types = popular_types
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'total_consultations': self.total_consultations,
            'active_users': self.active_users,
            'avg_response_length': self.avg_response_length,
            'popular_types': self.popular_types
        }

class UserStats:
    def __init__(self, total_users: int, today_users: int, consultation_stats: ConsultationStats):
        self.total_users = total_users
        self.today_users = today_users
        self.consultation_stats = consultation_stats
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            'total_users': self.total_users,
            'today_users': self.today_users,
            'total_consultations': self.consultation_stats.total_consultations,
            'active_users': self.consultation_stats.active_users,
            'avg_response_length': self.consultation_stats.avg_response_length,
            'popular_types': self.consultation_stats.popular_types
        }